﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XsClient.Common;
using XsClient.Parsers;

namespace XsClient.Controls
{
    /// <summary>
    /// UCCharpers.xaml 的交互逻辑
    /// </summary>
    public partial class UCCharpers : UserControl
    {
        public delegate void CharperSelect(BookInfo book, ChaperInfo chaper);

        /// <summary>
        /// 选择章节
        /// </summary>
        public event CharperSelect SelectCharpered;

        public UCCharpers()
        {
            InitializeComponent();
        }
        private BookInfo book = null;
        private List<ChaperInfo> chapers = null;
        private int selIndex = 0;

        public void SetData(BookInfo book)
        {
            this.book = book;
            //LoadChapers();
        }
        /// <summary>
        /// 跳到章节
        /// </summary>
        /// <param name="name"></param>
        public void MoveTo(string name)
        {
            try {
                
                if(this.chapers!=null&& this.chapers.Count > 0)
                {
                    int id = this.chapers.FindIndex(p => p.Name == name);
                    if (id > 0 && id < this.chapers.Count)
                    {
                        selIndex = id;
                        if (SelectCharpered != null)
                        {
                            SelectCharpered(book,chapers[selIndex]);
                        }
                        ScrollTo(id);
                    }
                }

            }catch(Exception ex)
            {

                NewLife.Log.XTrace.WriteException(ex);
            }
        }
        private void ScrollTo(int id)
        {

            double itemH = 25 + 1 * 2;// + 1 * 2;
            svPanel.ScrollToVerticalOffset(id * itemH);
        }
        public void SetData(List<ChaperInfo> chapers,string readName)
        {
            //  this.book = book;if (chapers != null && chapers.Count > 0)
            try
            {
                panel.Dispatcher.BeginInvoke(new Action(() =>
                {
                    panel.Children.Clear();
                    if (chapers != null && chapers.Count > 0)
                    {
                        this.chapers = chapers;
                        foreach (var item in chapers)
                        {
                            Label lb = new Label() { Margin = new Thickness(5, 1, 5, 1),Height=25,Padding=new Thickness(5,2,5,2), Content = item.Name, Tag = item, Foreground = Brushes.LightGray };
                            lb.ToolTip = item.Name;
                            lb.MouseLeftButtonDown += Lb_MouseLeftButtonDown;
                            panel.Children.Add(lb);
                        }
                        if (!string.IsNullOrEmpty(readName))
                            MoveTo(readName);
                    }
                }));
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }
        /// <summary>
        /// 下一个
        /// </summary>
        public void MoveNext()
        {

            try
            {
                if (selIndex >= 0 && selIndex < chapers.Count - 1)
                {
                    selIndex++;
                    if (SelectCharpered != null)
                    {
                        SelectCharpered(book, chapers[selIndex]);
                    }
                    ScrollTo(selIndex);
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);

            }
        }
        /// <summary>
        /// 上一个
        /// </summary>
        public void MovePrev()
        {
            try
            {
                if (selIndex > 0)
                {
                    selIndex--;
                    if (SelectCharpered != null)
                    {
                        SelectCharpered(book, chapers[selIndex]);
                    }
                    ScrollTo(selIndex);
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);

            }
        }

        private void LoadChapers()
        {
            try
            {
                panel.Children.Clear();
                var resolver = ResolverFactory.GetResolver(book.SourceID);
                string bookUrl = book.BaseUrl;
                chapers = resolver.FindChapers(ref bookUrl);
                if (chapers != null && chapers.Count > 0)
                {
                    if (bookUrl != book.BaseUrl)
                        book.BaseUrl = bookUrl;

                    int i = 0;
                    foreach (var item in chapers)
                    {
                        Label lb = new Label() { Content = item.Name, Tag = i, ToolTip = item.Name };
                        lb.MouseLeftButtonDown += Lb_MouseLeftButtonDown;
                        panel.Children.Add(lb);
                        i++;
                    }
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void Lb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                Label lb = sender as Label;
                if (lb != null)
                {
                    int index = panel.Children.IndexOf(lb);
                    int i = index;// int.Parse(lb.Tag.ToString());
                    selIndex = i;
                    if (SelectCharpered != null)
                    {
                        SelectCharpered(book, chapers[selIndex]);
                    }
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);

            }
        }
    }
}
